﻿using Common.Device.Interfaces;
using Common.Device.Views.Devices;
using HandyControl.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Common.Device.Views.Dialogs
{
    /// <summary>
    /// DeviceDebugView.xaml 的交互逻辑
    /// </summary>
    public partial class DeviceDebugView : UserControl
    {
        //弹窗
        static HandyControl.Controls.PopupWindow window;
        private DeviceDebugView()
        {
            InitializeComponent();
        }
        //调试界面
        public static void Show(string name = "")
        {
            try
            {
                DeviceDebugView view = new DeviceDebugView();
                ViewModels.DeviceDebugViewModel viewModel = new ViewModels.DeviceDebugViewModel();
                view.DataContext = viewModel;
                viewModel.document = view.richTextBox.RichDocument;
                window = new HandyControl.Controls.PopupWindow
                {
                    ShowBorder = false,
                    ShowTitle = false,
                    Background = Brushes.Transparent,
                    PopupElement = view,//将当前用户界面加载到弹窗窗口中实现弹窗功能
                    WindowStartupLocation = WindowStartupLocation.CenterScreen,
                    AllowsTransparency = true,
                    WindowStyle = WindowStyle.None,
                    MinWidth = 0,
                    MinHeight = 0
                };
                view.Loaded += (s, e) =>
                {
                    viewModel.DeviceName = name;
                    viewModel.Device = DeviceService.useDevices[name];
                };
                view.names.SelectionChanged += (s, e) =>
                {
                    //仪器切换时刷新界面
                    viewModel.Device = DeviceService.useDevices[viewModel.DeviceNames[view.names.SelectedIndex]];
                    viewModel.DeviceView = null;
                    var idevice = DeviceService.DeviceFactory(viewModel.Device, viewModel.document);
                    switch (viewModel.Device.type)
                    {
                        case Enums.DeviceEnum.DeviceTypeEnum.绝缘耐压:
                            viewModel.DeviceView = new JueYuanNaiYaView(viewModel.Device.model);
                            (viewModel.DeviceView as JueYuanNaiYaView).Device = idevice as IJueYuanNaiYa; break;
                        case Enums.DeviceEnum.DeviceTypeEnum.温度采集:
                            viewModel.DeviceView = new CaiJiMoKuaiView(viewModel.Device.model);
                            (viewModel.DeviceView as CaiJiMoKuaiView).Device = idevice as ICaiJiMoKuai; break;
                        case Enums.DeviceEnum.DeviceTypeEnum.电压采集:
                            viewModel.DeviceView = new DianYaCaiJiView(viewModel.Device.model);
                            (viewModel.DeviceView as DianYaCaiJiView).Device = idevice as IDianYaCaiJi; break;
                        case Enums.DeviceEnum.DeviceTypeEnum.电阻仪:
                            viewModel.DeviceView = new CeLiangYiView(viewModel.Device.model);
                            (viewModel.DeviceView as CeLiangYiView).Device = idevice as IDianZuYi; break;
                        case Enums.DeviceEnum.DeviceTypeEnum.数字电桥:
                            viewModel.DeviceView = new CeLiangYiView(viewModel.Device.model);
                            (viewModel.DeviceView as CeLiangYiView).Device = idevice as IShuZiDianQiao; break;
                        case Enums.DeviceEnum.DeviceTypeEnum.万用表:
                            viewModel.DeviceView = new CeLiangYiView(viewModel.Device.model);
                            (viewModel.DeviceView as CeLiangYiView).Device = idevice as IWanYongBiao; break;
                        case Enums.DeviceEnum.DeviceTypeEnum.交流电源:
                        case Enums.DeviceEnum.DeviceTypeEnum.直流电源:
                            viewModel.DeviceView = new DianYuanView(viewModel.Device.model);
                            (viewModel.DeviceView as DianYuanView).Device = idevice as IDianYuan; break;
                        case Enums.DeviceEnum.DeviceTypeEnum.电子负载:
                            viewModel.DeviceView = new DianZiFuZaiView(viewModel.Device.model);
                            (viewModel.DeviceView as DianZiFuZaiView).Device = idevice as IDianZiFuZai; break;
                        case Enums.DeviceEnum.DeviceTypeEnum.示波器:
                            viewModel.DeviceView = new ShiBoQiView(viewModel.Device.model);
                            (viewModel.DeviceView as ShiBoQiView).Device = idevice as IShiBoQi; break;
                        case Enums.DeviceEnum.DeviceTypeEnum.FCT测试板:
                            viewModel.DeviceView = new FctView(viewModel.Device);
                            (viewModel.DeviceView as FctView).Device = idevice as IFct; break;
                        case Enums.DeviceEnum.DeviceTypeEnum.FCT测试板3:
                            viewModel.DeviceView = new FctView(viewModel.Device);
                            (viewModel.DeviceView as FctView).Device = idevice as IFct3; break;
                        case Enums.DeviceEnum.DeviceTypeEnum.通信测试:
                            viewModel.DeviceView = new TongXinView();
                            (viewModel.DeviceView as TongXinView).Device = idevice as ITongXin; break;
                        case Enums.DeviceEnum.DeviceTypeEnum.车身域:
                            viewModel.DeviceView = new GeChaoJiCheView(viewModel.Device.model);
                            (viewModel.DeviceView as GeChaoJiCheView).Device = idevice as IGeChaoJi; break;
                        case Enums.DeviceEnum.DeviceTypeEnum.智控域:
                            viewModel.DeviceView = new ZhiKongView(viewModel.Device.model);
                            (viewModel.DeviceView as ZhiKongView).Device = idevice as IZhiKong; break;
                        default:
                            break;
                    }
                    //清空日志
                    view.richTextBox.Document.Blocks.Clear();
                    view.richTextBox.ScrollToVerticalOffset(0);
                };
                window.Show();
            }
            catch (Exception ex)
            {
                HandyControl.Controls.MessageBox.Error(ex.ToString());
            }

        }

        private void Grid_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
                //触摸屏触发时跳过
                if (e.StylusDevice != null)
                {
                    e.Handled = true;
                }
                else
                {
                    window?.DragMove();
                }
            }
            catch (Exception)
            {

                //throw;
            }

        }
        //触摸屏触发
        private void Grid_PreviewTouchDown(object sender, TouchEventArgs e)
        {
            try
            {
                window?.DragMove();
            }
            catch (Exception)
            {

                // throw;
            }

        }
        private void closeClick(object sender, RoutedEventArgs e)
        {
            window?.Close();
        }


    }
}
